<?php

namespace App\V3\Api\Controllers;

use App\V3\Api\Models\AdminAddendumModel;
use App\V3\Api\Models\VipSettingModel;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Storage;
use Mavinoo\Batch\Batch;
use App\V3\Api\Models\StorageSpaceOrderModel;
use Illuminate\Support\Facades\DB;


class CrontabController extends Controller
{
    public function endExperience()
    {
        $userInstance = new AdminAddendumModel;

        DB::enableQueryLog();
        $adminInfo = AdminAddendumModel::where('is_vip', '!=', 0)
            ->where('vip_end_date', '<=', time())
            ->get()->toArray();

        $notVipInfo = VipSettingModel::where('is_vip', 0)->first();
        $isVipInfo = VipSettingModel::where('is_vip', 1)->first();

        $distanceSpace = $isVipInfo['account_storage'] - $notVipInfo['account_storage'];

        $arr = [];
        foreach($adminInfo as $value) {
            if($value['account_storage'] > $notVipInfo['account_storage']) {
                $arr[] = [
                    'id' => $value['id'],
                    'is_vip' => 0,
                    'is_in_experience' => 1,
                    'is_end_experience' => 1,
                    'album_amount' => $notVipInfo['album_amount'],
                    'album_page_amount' => $notVipInfo['album_page_amount'],
                    'empty_bill_amount' => $notVipInfo['empty_bill_amount'],
                    'special_seal' => $notVipInfo['special_seal'],
                    'one_key_album_times' => $notVipInfo['one_key_album_times'],
                    'express_check_times' => $notVipInfo['express_check_times'],
                    'account_storage' => $value['account_storage'] - $distanceSpace,
                    'clear_watermark_or_not' => $notVipInfo['clear_watermark_or_not'],
                    'sub_account_number' => $notVipInfo['sub_account_number'],
                ];
            }
            else {
                $arr[] = [
                    'id' => $value['id'],
                    'is_vip' => 0,
                    'is_in_experience' => 1,
                    'is_end_experience' => 1,
                    'album_amount' => $notVipInfo['album_amount'],
                    'album_page_amount' => $notVipInfo['album_page_amount'],
                    'empty_bill_amount' => $notVipInfo['empty_bill_amount'],
                    'special_seal' => $notVipInfo['special_seal'],
                    'one_key_album_times' => $notVipInfo['one_key_album_times'],
                    'express_check_times' => $notVipInfo['express_check_times'],
                    'account_storage' => $notVipInfo['account_storage'],
                    'clear_watermark_or_not' => $notVipInfo['clear_watermark_or_not'],
                    'sub_account_number' => $notVipInfo['sub_account_number'],
                ];
            }
        }

        if(!empty($arr)) {
            batch()->update($userInstance, $arr, 'id');
        }

        return true;
    }

    public function accountStorageStatus(){
        $arr1 = [];
        $arr2 = [];
        $t=time()-365*24*3600;
        $adminAddendumInstance = new AdminAddendumModel;
        $storageSpaceOrderInstance = new StorageSpaceOrderModel;
        $ssList = $storageSpaceOrderInstance::select(DB::raw('SUM(buy_storage_space) as total_space'),'main_id')
            ->where('pay_time','<=',$t)->where('is_expire',0)->where('status',1)->groupBy('main_id')->get()->toArray();

        if(!empty($ssList)){
            foreach ($ssList as $v){
                $adminInfo = $adminAddendumInstance::select('account_storage')->where('main_id',$v['main_id'])->first();
                $account_storage = $adminInfo['account_storage']-$v['total_space']*1024*1024;
                $arr1[] = [
                    'main_id' => $v['main_id'],
                    'account_storage' => $account_storage
                ];
                $arr2[] = [
                    'id' => $v['id'],
                    'is_expire' => 1
                ];
            }

            if(!empty($arr1)){
                batch()->update($adminAddendumInstance, $arr1, 'main_id');
            }
            if(!empty($arr2)){
                batch()->update($storageSpaceOrderInstance, $arr2, 'id');
            }
        }

        return true;
    }
}
